package com.czr.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.czr.constant.isLogin;
import com.czr.dao.entity.CzrBusi;
import com.czr.dao.entity.CzrCompany;
import com.czr.dao.service.CzrBusiService;
import com.czr.dao.service.CzrCompanyService;
import com.czr.dto.CompanyParam;
import com.czr.dto.CzrBusiParam;
import com.czr.dto.Search;
import com.czr.dto.StatusParam;
import com.czr.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@Api(value = "名企推荐", tags = "名企推荐")
@RestController
@RequestMapping("/busi")
public class CzrBusiController {

    @Autowired
    private CzrBusiService czrBusiService;

    @Autowired
    private CzrCompanyService czrCompanyService;

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @isLogin
    @ApiOperation(value = "名企推荐列表", notes = "名企推荐列表")
    @PostMapping("/list")
    public Result<Object> list(@RequestBody CompanyParam companyParam) {
        try {
            return czrCompanyService.list(companyParam);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }

    @ApiOperation(value = "名企推荐申请", notes = "名企推荐申请")
    @PostMapping("/add")
    public Result<Map<String, Object>> add(@RequestBody CzrBusiParam czrBusiParam, HttpServletRequest request) {
        try {

            ValueOperations<Object, Object> valueOperations = redisTemplate.opsForValue();
            Object object=valueOperations.get(request.getHeader("token"));
            if (!StringUtils.isEmpty(object)) {
                JSONObject cacheToken = JSON.parseObject(object.toString());
                return  czrBusiService.add(czrBusiParam,Integer.parseInt(cacheToken.get("id").toString()));
            }else{
                return Result.fail("token异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }
    @ApiOperation(value = "管理端-企业申请信息处理", notes = "企业申请信息处理")
    @PostMapping("/examine")
    public Result<Map<String, Object>> examine(@RequestBody CzrBusi czrBusi) {
        try {
            Result<Map<String, Object>> result = czrBusiService.update(czrBusi);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }

    @ApiOperation(value = "管理端-名企申请列表", notes = "管理端-名企申请列表")
    @PostMapping("/glList")
    public Result<Object> glList(@RequestBody StatusParam statusParam) {
        try {
            return czrBusiService.glList(statusParam);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }

    @ApiOperation(value = "管理端-名企推荐列表", notes = "管理端-名企推荐列表")
    @PostMapping("/glCompanyList")
    public Result<Object> glCompanyList(@RequestBody CompanyParam companyParam) {
        try {
            return czrCompanyService.glCompanyList(companyParam);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }
    @ApiOperation(value = "管理端-企业录入", notes = "管理端-企业录入")
    @PostMapping("/glAdd")
    public Result<Map<String, Object>> glAdd(@RequestBody CzrCompany czrCompany, HttpServletRequest request) {
        try {

            ValueOperations<Object, Object> valueOperations = redisTemplate.opsForValue();
            Object object=valueOperations.get(request.getHeader("token"));
            if (!StringUtils.isEmpty(object)) {
                JSONObject cacheToken = JSON.parseObject(object.toString());
                return  czrCompanyService.add(czrCompany,Integer.parseInt(cacheToken.get("id").toString()));
            }else{
                return Result.fail("token异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("失败");
        }

    }
    @ApiOperation(value = "管理端-删除名企推荐", notes = "管理端-删除名企推荐")
    @GetMapping("/delCompany")
    public Result<Map<String, Object>> delCompany(@RequestParam Integer companyId, HttpServletRequest request) {
        try {
            ValueOperations<Object, Object> valueOperations = redisTemplate.opsForValue();
            Object object=valueOperations.get(request.getHeader("token"));
            if (!StringUtils.isEmpty(object)) {
                JSONObject cacheToken = JSON.parseObject(object.toString());
                return  czrCompanyService.delCompany(companyId,cacheToken);
            }else{
                return Result.fail("token异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }

    @ApiOperation(value = "管理端-删除申请信息", notes = "管理端-删除申请信息")
    @GetMapping("/delBusi")
    public Result<Map<String, Object>> delBusi(@RequestParam Integer busiId, HttpServletRequest request) {
        try {
            ValueOperations<Object, Object> valueOperations = redisTemplate.opsForValue();
            Object object=valueOperations.get(request.getHeader("token"));
            if (!StringUtils.isEmpty(object)) {
                JSONObject cacheToken = JSON.parseObject(object.toString());
                return  czrBusiService.delBusi(busiId,cacheToken);
            }else{
                return Result.fail("token异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }

}
